package cmq

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
)

const QueueNotExistCode = 4440

// 创建队列
func (sdk *TencentSDK) AddQueue(queueName string) error {
	url := sdk.Pipeline("CreateQueue", queueName, "", "")
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	ret := new(sdkRet)
	err = json.Unmarshal(body, ret)
	if err != nil {
		return err
	}
	if ret.Code != 0 {
		return errors.New(ret.Message)
	}

	return nil
}

// 获取队列列表
func (sdk *TencentSDK) GetQueueList() ([]*queueRet, error) {
	url := sdk.Pipeline("ListQueue", "", "", "")
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	ret := new(sdkRet)
	err = json.Unmarshal(body, ret)
	if err != nil {
		return nil, err
	}
	if ret.Code != 0 {
		return nil, errors.New(ret.Message)
	}

	return ret.QueueList, nil
}

// 获取队列剩余未消费消息数
func (sdk *TencentSDK) GetMsgCount(queueName string) (int, error) {
	url := sdk.Pipeline("GetQueueAttributes", queueName, "", "")
	resp, err := http.Get(url)
	if err != nil {
		return 0, err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return 0, err
	}

	ret := new(sdkRet)
	err = json.Unmarshal(body, ret)
	if err != nil {
		return 0, err
	}
	if ret.Code == QueueNotExistCode {
		return 0, QueueIsNotExistErr
	}
	if ret.Code != 0 {
		return 0, errors.New(ret.Message)
	}

	return ret.ActiveMsgNum, nil
}

// 删除队列
func (sdk *TencentSDK) DeleteQueue(queueName string) error {
	url := sdk.Pipeline("DeleteQueue", queueName, "", "")
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	ret := new(sdkRet)
	err = json.Unmarshal(body, ret)
	if err != nil {
		return err
	}
	if ret.Code == QueueNotExistCode {
		return QueueIsNotExistErr
	}
	if ret.Code != 0 {
		return errors.New(ret.Message)
	}

	return nil
}

// 推送消息
func (sdk *TencentSDK) PushMsg(queueName, msg string) error {
	url := sdk.Pipeline("SendMessage", queueName, msg, "")
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	ret := new(sdkRet)
	err = json.Unmarshal(body, ret)
	if err != nil {
		return err
	}
	if ret.Code == QueueNotExistCode {
		return QueueIsNotExistErr
	}
	if ret.Code != 0 {
		return errors.New(ret.Message)
	}

	return nil
}

// 拉取消息
func (sdk *TencentSDK) GetMsg(queueName string) (msg, msgHandle string, err error) {
	url := sdk.Pipeline("ReceiveMessage", queueName, "", "")
	resp, err := http.Get(url)
	if err != nil {
		return "", "", err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", "", err
	}

	ret := new(sdkRet)
	err = json.Unmarshal(body, ret)
	if err != nil {
		return "", "", err
	}
	if ret.Code == QueueNotExistCode {
		return "", "", QueueIsNotExistErr
	}
	if ret.Code != 0 {
		return "", "", errors.New(ret.Message)
	}

	return ret.MsgBody, ret.ReceiptHandle, nil
}

// 删除消息
func (sdk *TencentSDK) DeleteMsg(queueName, msgHandle string) error {
	url := sdk.Pipeline("DeleteMessage", queueName, "", msgHandle)
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	ret := new(sdkRet)
	err = json.Unmarshal(body, &ret)
	if err != nil {
		return err
	}
	if ret.Code == QueueNotExistCode {
		return QueueIsNotExistErr
	}
	if ret.Code != 0 {
		return errors.New(ret.Message)
	}

	return nil
}

// 初始化参数
func (sdk *TencentSDK) Init(secretId, secretKey, serverUrl, region string) {
	sdk.secretId = secretId
	sdk.secretKey = secretKey
	sdk.serverUrl = serverUrl
	sdk.region = region
}

func IsNotExist(err error) bool {
	if err == QueueIsNotExistErr {
		return true
	}
	return false
}
